//package com.gzdh.tang.modu.job;
//
//import com.alibaba.fastjson.JSONObject;
//import com.alipay.api.AlipayClient;
//import com.alipay.api.DefaultAlipayClient;
//import com.alipay.api.request.AlipayTradeQueryRequest;
//import com.alipay.api.response.AlipayTradeQueryResponse;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.gzdh.tang.config.ali.AliPayUtil;
//import com.gzdh.tang.config.weixin.WXEntity;
//import com.gzdh.tang.config.weixin.WXPay;
//import com.gzdh.tang.config.weixin.WXPayUtils;
//import com.gzdh.tang.modu.entity.Addr;
//import com.gzdh.tang.modu.entity.TangOrder;
//import com.gzdh.tang.modu.service.TangAddrService;
//import com.gzdh.tang.modu.service.TangOrderService;
//import com.xxl.job.core.biz.model.ReturnT;
//import com.xxl.job.core.handler.IJobHandler;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//import static com.xxl.job.core.biz.model.ReturnT.SUCCESS;
//
//@JobHandler(value = "OrderCheckAliJobHandler")
//@Component
//public class OrderCheckAliJob extends IJobHandler {
//
//
//    @Autowired
//    TangOrderService tangOrderService;
//    @Autowired
//    TangAddrService addrService;
//
//    @Override
//    public ReturnT<String> execute(String s) throws Exception {
//        List<TangOrder> tangOrderList = tangOrderService.list(new QueryWrapper<TangOrder>()
//                .lambda().eq(TangOrder::getPayChannel, 3)
//                .eq(TangOrder::getPayState, 0));
//        XxlJobLogger.log("查询到未知订单条数：【" + tangOrderList.size() + "】");
//        for (TangOrder goo : tangOrderList) {
//            checkOrder(goo, new Addr());
//        }
//        return SUCCESS;
//    }
//
//    private void checkOrder(TangOrder tangOrder, Addr addr) {
//        try {
////            WXEntity wxEntity = new WXEntity();
////            WXPay wxPay = new WXPay(wxEntity);
////            //start pay
////            Map<String, String> reqMap = new HashMap<>();
////            reqMap.put("out_trade_no", tangOrder.getOrders());
////            reqMap.put("sub_mch_id", WXPayUtils.getSubMchId());
////            Map<String, String> resMap = wxPay.orderQuery(reqMap);
////            if (!"SUCCESS".equals(resMap.get("return_code")) && !"SUCCESS".equals(resMap.get("return_code"))) {
////                XxlJobLogger.log(tangOrder.getOrders() + "支付系统错误，需要进行查单");
////            } else if ("SUCCESS".equals(resMap.get("trade_state")) || "ACCEPT".equals(resMap.get("trade_state"))) {
////                //支付成功订单
////                XxlJobLogger.log(tangOrder.getOrders() + "支付成功");
////                tangOrderService.updateOrderPayState(tangOrder.getId(), 1);
////            } else if ("USERPAYING".equals(resMap.get("trade_state"))) {
////                XxlJobLogger.log(tangOrder.getOrders() + "等待用户输入密码");
////            } else {
////                XxlJobLogger.log(tangOrder.getOrders() + "支付失败");
////                tangOrderService.updateOrderPayState(tangOrder.getId(), 3);
////            }
//
//            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
//                    AliPayUtil.getAppid(), AliPayUtil.getPrivateKey(),
//                    "json", "GBK", AliPayUtil.getPublicKey(),
//                    "RSA2");
//            AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("out_trade_no", tangOrder.getOrders());
//            request.setBizContent(jsonObject.toJSONString());
//            AlipayTradeQueryResponse response = alipayClient.execute(request, null, AliPayUtil.getAppAuthToken());
//            if (response.isSuccess()) {
//                System.out.println("调用成功");
//                if ("TRADE_SUCCESS".equals(response.getTradeStatus())) {
//                    tangOrderService.updateOrderPayState(tangOrder.getId(), 1);
//                } else {
//                    tangOrderService.updateOrderPayState(tangOrder.getId(), 3);
//                }
//            } else {
//                System.out.println("调用失败");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}
